package com.example.foobar.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.foobar.business.service.DaOfficialAccountService;
import com.example.foobar.business.service.ImsGhostQrcodesService;
import com.example.foobar.dao.service.ImsGhostQrcodesDaoService;
import com.example.foobar.entity.DaOfficialAccount;
import com.example.foobar.entity.DaQrcodes;
import com.example.foobar.entity.ImsGhostQrcodes;
import io.geekidea.springbootplus.framework.util.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 分析系统配置文件 服务实现类
 *
 * @author 郑英奔
 * @since 2021-05-31
 */
@Slf4j
@Service
public class ImsGhostQrcodesServiceImpl implements ImsGhostQrcodesService {

    @Autowired
    ImsGhostQrcodesDaoService imsGhostQrcodesDaoService;

    @Autowired
    DaOfficialAccountService daOfficialAccountService;

    @Override
    public boolean saveImsGhostQrcodes(ImsGhostQrcodes imsGhostQrcodes) {
        return imsGhostQrcodesDaoService.saveImsGhostQrcodes(imsGhostQrcodes);
    }

    @Override
    public boolean updateImsGhostQrcodes(ImsGhostQrcodes imsGhostQrcodes) {
        return imsGhostQrcodesDaoService.updateImsGhostQrcodes(imsGhostQrcodes);
    }

    @Override
    public boolean deleteImsGhostQrcodes(Long id) {
        return imsGhostQrcodesDaoService.deleteImsGhostQrcodes(id);
    }

    @Override
    public ImsGhostQrcodes getById(Long id) {
        return imsGhostQrcodesDaoService.getById(id);
    }

    @Override
    public List<ImsGhostQrcodes> selectList(QueryWrapper<ImsGhostQrcodes> imsAccountWechatsLambdaQueryWrapper) {
        return imsGhostQrcodesDaoService.selectList(imsAccountWechatsLambdaQueryWrapper);
    }

    @Override
    public List<ImsGhostQrcodes> list() {
        List<ImsGhostQrcodes> list = imsGhostQrcodesDaoService.list();
        if(LoginUtil.getUserId() == 1){
            return list;
        }else {

            return filterDaQrcode(list);
        }

    }

    private List<ImsGhostQrcodes>  filterDaQrcode(List<ImsGhostQrcodes> list) {
        List<ImsGhostQrcodes> result = new ArrayList<>();
        Long userId = LoginUtil.getUserId();
        List<DaOfficialAccount> daOfficialAccounts = daOfficialAccountService.list(userId);
        List<Integer> uniacidList = new ArrayList<>();
        daOfficialAccounts.forEach(daOfficialAccount -> {
            uniacidList.add(daOfficialAccount.getUniacid());
        });

        list.forEach(daQrcodes -> {
            if(uniacidList.contains(daQrcodes.getUniacid())){
                result.add(daQrcodes);
            }
        });
        return result;
    }
}
